home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / stsim2.zip / STOCK.PAS < prev    next >
Pascal/Delphi Source File  |  1993-04-01  |  7KB  |  353 lines

  1. uses crt,menu,graph,sunit;
  2. { type
  3.         date = record
  4.                         month:byte;
  5.                         day:byte;
  6.                         year:word;
  7.                end;
  8.  
  9.         dayptr = ^day;
  10.         day = record
  11.                 price:real;
  12.                 thing:integer;
  13.                         1 to 9999 is stock,
  14.                         0 is player
  15.                         -99 is stock market average.
  16.  
  17.                 next:dayptr;
  18.               end;
  19.  
  20.         stock = record
  21.                         symbol : string[3];
  22.                         shares : integer;
  23.                 end;
  24.  
  25.         company = record
  26.                         name : string;
  27.                         symbol : string[3];
  28.                         cash : real;
  29.                         stock_price : real;
  30.                   end;
  31. var
  32.         universal_date:date;
  33.         first_one,current:dayptr;
  34.  
  35.         co:array[1..20] of company;
  36.         tmp_month:integer;
  37.         num_co:integer;
  38.         _flash:integer;
  39.         _flash_str:string;
  40. }
  41.  
  42. type
  43.     rplayer = record
  44.             stock:array[1..10] of stock;
  45.             num_stock:integer;
  46.             cash:real;
  47.           end;
  48. var
  49.     num_rec:integer;
  50.     cur_month:integer;
  51.     player:rplayer;
  52.  
  53. procedure transact;forward;
  54.  
  55. procedure del_r(var d:dayptr);
  56. var    x:dayptr;
  57. begin
  58.     x:=d;
  59.     d:=d^.next;
  60.     dispose(x);
  61. end;
  62. function stock_sum:real;forward;
  63.  
  64. function dow_jones:real;
  65. var
  66.     total:real; i:integer;
  67. begin
  68.     total:=0;
  69.     for i:=1 to num_co do begin
  70.         total:=total+co[i].stock_price;
  71.     end;
  72.     dow_jones:=total / num_co;
  73. end;
  74.  
  75. procedure date_add;
  76. var i:integer;
  77. begin
  78.         with universal_date do
  79.         begin
  80.                 inc(day);
  81.                 if day>30 then begin
  82.                         day:=1;
  83.                         inc(month);
  84.                 end;
  85.  
  86.                 if month>12 then begin
  87.                         month:=1;
  88.                         inc(year);
  89.                 end;
  90.                 gotoxy(70,3); write(month:2,'/',day:2,'/',year:4);
  91.         end;
  92.  
  93.         for i:=1 to num_co do begin
  94.                 with current^ do begin
  95.                         thing:=i;
  96.                         price:=co[i].stock_price;
  97.                 end;
  98.         new(current^.next);
  99.         current:=current^.next;
  100.         current^.next:=NIL;
  101.         if num_rec>200 then begin
  102.             del_r(first_one);
  103.         end;
  104.     end;
  105.                 with current^ do begin
  106.             thing:=0;
  107.             price:=stock_sum+player.cash;
  108.                 end;
  109.         new(current^.next);
  110.         current:=current^.next;
  111.         current^.next:=NIL;
  112.         if num_rec>200 then begin
  113.             del_r(first_one);
  114.         end;
  115.  
  116.                 with current^ do begin
  117.             thing:=-99;
  118.             price:=dow_jones;
  119.                 end;
  120.         new(current^.next);
  121.         current:=current^.next;
  122.         current^.next:=NIL;
  123.         if num_rec>200 then begin
  124.             del_r(first_one);
  125.         end;
  126.     inc(num_rec);
  127. end;
  128.  
  129. procedure display;forward;
  130. procedure _graph;
  131. const
  132.     left=50;
  133. var
  134.     grd,grm,c,i,divisor:integer;
  135.     tmp:dayptr;
  136.     s:string;
  137.  
  138. begin
  139.     s:='';
  140.     for c:=1 to num_co do with co[c] do begin
  141.         s:=s+symbol+',';
  142.     end;
  143.     s:=s+'Player,Dow Average';
  144.  
  145.     s:=menu1(1,1,s);
  146.     for c:=1 to num_co do
  147.         if co[c].symbol=s then i:=c;
  148.     if s='Player' then i:=0;
  149.     if s='Dow Average' then i:=-99;
  150.     textattr:=7;
  151.  
  152.  
  153.     grd:=detect;
  154.     initgraph(grd,grm,'');
  155.     line(left,1,left,getmaxy);
  156.     for c:=getmaxy downto 1 do
  157.       if (c mod 20) = 0 then begin
  158.         str(c,s);
  159.         s:='$'+s;
  160.  
  161.         outtextxy(1,getmaxy-c,s);
  162.       end;
  163.  
  164.     for c:=getmaxx downto 1 do
  165.       if (c mod 50) = 0 then begin
  166.         str(round(c/1.2),s);
  167.         outtextxy(c,getmaxy-10,s);
  168.       end;
  169.  
  170.  
  171.     tmp:=first_one;
  172.  
  173.     c:=0;
  174.     divisor:=1;
  175.     if i=0 then divisor:=20;
  176.         repeat
  177.           if tmp^.thing=i then begin
  178.            if c=0 then
  179.         moveto(c+left,(getmaxy-round(tmp^.price/divisor))) else
  180.         lineto(c+left,(getmaxy-round(tmp^.price/divisor)));
  181.         inc(c);
  182.           end;
  183.           tmp:=tmp^.next;
  184.     until tmp=NIL;
  185.     repeat until keypressed;
  186.     closegraph;
  187. end;
  188.  
  189. procedure time_proc;
  190. var
  191.         x,y:integer;
  192. begin
  193.                         x:=wherex;y:=wherey;
  194.  
  195.             flash;
  196.                         stock_window(3,4);
  197.             transact;
  198.             display;
  199.             date_add;
  200.  
  201.             gotoxy(70,4);
  202.             write(memavail);
  203.  
  204.             if universal_date.month<>cur_month then begin
  205.                 earn;
  206.                 cur_month:=universal_date.month;
  207.             end;
  208.  
  209.                         gotoxy(x,y);
  210. end;
  211.  
  212. procedure transact;
  213. var i:integer;
  214. begin
  215.     for i:=1 to num_co do with co[i] do begin
  216.         if random(2)=0 then
  217.             stock_price:=stock_price+random(3) else
  218.             stock_price:=stock_price-random(3);
  219.     end;
  220. end;
  221.  
  222. function priceof(s:string):real;
  223. var i:integer;
  224. begin
  225.     for i:=1 to num_co do
  226.         if co[i].symbol=s then priceof:=co[i].stock_price;
  227. end;
  228.  
  229. function stock_sum:real;
  230. var total:real;i:integer;
  231. begin
  232.     total:=0;
  233.     with player do begin
  234.         for i:=1 to num_stock do
  235.             total:=total+(priceof(stock[i].symbol)*stock[i].shares)
  236.     end;
  237.     stock_sum:=total;
  238. end;
  239.  
  240. procedure display;
  241. begin
  242.     box(60,10,79,15);
  243.     gotoxy(61,11);
  244.     write('Cash=>',player.cash:7:2);
  245.     gotoxy(61,12);
  246.     write('Stock=>',stock_sum:7:2);
  247.     gotoxy(61,13);
  248.     write('Dow=>',dow_jones:7:2);
  249. end;
  250.  
  251. procedure pbuy;
  252. var s:string;
  253.     c,i:integer;
  254. begin
  255.     s:='';
  256.     for c:=1 to num_co-1 do with co[c] do begin
  257.         s:=s+symbol+',';
  258.     end;
  259.     s:=s+co[num_co].symbol;
  260.  
  261.     s:=menu1(1,1,s);
  262.     for c:=1 to num_co do
  263.         if co[c].symbol=s then begin
  264.             writeln;writeln;
  265.             writeln('How many shares?');
  266.             readln(i);
  267.             with player do
  268.             if (i*co[c].stock_price)<cash then begin
  269.                 inc(num_stock);
  270.                 stock[num_stock].shares:=i;
  271.                 stock[num_stock].symbol:=s;
  272.                 cash:=cash-(i*co[c].stock_price);
  273.             end;
  274.         end;
  275. end;
  276.  
  277. procedure psell;
  278. var s:string;
  279.     c,i:integer;
  280. begin
  281.     with player do begin
  282.     s:='';
  283.     for c:=1 to num_stock do with stock[c] do begin
  284.         s:=s+symbol+',';
  285.     end;
  286.  
  287.     s:=menu1(1,1,s);
  288.     for c:=1 to num_stock do
  289.         if stock[c].symbol=s then begin
  290.             writeln;writeln;
  291.             writeln('How many shares?');
  292.             readln(i);
  293.             if stock[c].shares<=i then begin
  294.                 stock[c].shares:=stock[c].shares-i;
  295.                 cash:=cash+(stock[c].shares*co[c].stock_price);
  296.             end;
  297.     end;
  298.     end;
  299. end;
  300.  
  301. procedure init_play;
  302. begin
  303.     player.cash:=5000;
  304.     player.num_stock:=0;
  305. end;
  306.  
  307. procedure init_commands;
  308. begin
  309.     box(1,17,30,25);
  310.     window(2,18,29,24);
  311.     gotoxy(1,1);
  312.     textattr:=2;
  313.     writeln('Commands:');
  314.     textattr:=7;
  315.     writeln('g-graph company,player,dow');
  316.     writeln('b-buy stock of company');
  317.     writeln('s-sell stock of company');
  318.     writeln('q-quit simulation');
  319.     window(1,1,80,25);
  320. end;
  321.  
  322. label rep;
  323. var
  324.         timer:word;
  325.     i:integer;
  326.     ch:char;
  327.  
  328. begin
  329.     textattr:=7;
  330.     randomize;
  331.     init;
  332.         init_stocks;
  333.     init_flash;
  334.     init_play;
  335.      rep:
  336.         clrscr;
  337.         gotoxy(50,20);
  338.     write('Command=>');
  339.     init_commands;
  340.         repeat
  341.                 inc(timer);
  342.         if timer>2000 then begin
  343.                         time_proc;
  344.             timer:=0;
  345.         end;
  346.     until keypressed;
  347.     ch:=readkey;
  348.     if ch='g' then _graph;
  349.     if ch='b' then pbuy;
  350.     if ch='s' then psell;
  351.     if ch='q' then else goto rep;
  352. end.
  353.